#get version from git repo
VER=${VER}
if [ "$VER" = "" ]; then
    VER=`git log -1 --pretty=format:%cd-%h --date=short`
    if [ $? != 0 ]; then
      echo "must provide ver or in a git repository"
      exit -1
    fi
fi

#app name
APPNAME=${APP:-lottery}
WORKDIR=${WORKDIR:-usr/local/apache-tomcat-8.0.26}

BUILD=${BUILD:-TRUE}
echo $APPNAME:$VER

Dockerfile=$(cat <<EOF
FROM xl.reg.com:5000/basetomcat
ADD ./target/lottery.war /$WORKDIR/webapps/
WORKDIR /$WORKDIR/bin
EXPOSE 8080
CMD /etc/init.d/ssh start && ./startup.sh  && tail -f /dev/null
EOF
)


if [ "$BUILD" = "TRUE" ] ;then 
    #update base image
    docker pull xl.reg.com:5000/basetomcat
    echo -e "$Dockerfile" > Dockerfile
    #build
    docker build -t xl.reg.com:5000/$APPNAME:$VER . 
    #push to docker registry
    docker push xl.reg.com:5000/$APPNAME:$VER
    #clean
    rm -rf ./target
    rm -f Dockerfile
fi
